﻿using Microsoft.AspNetCore.Mvc;
using Sgr.AspNetCore.ActionFilters.AuditLogs;
using Sgr.AspNetCore.ActionFilters.Permissions;
using Sgr.DataCategories.Services;

namespace Sgr.DataCategories.Controllers
{
    [Route("api/v1/sgr/adm-div")]
    [ApiController]
    [Produces("application/json")]
    [Tags("AdmDiv")]
    public class AdmDivController : ControllerBase
    {
        private readonly IAdmDivManagement _admDivQuery;

        public AdmDivController(IAdmDivManagement admDivQuery)
        {
            _admDivQuery = admDivQuery ?? throw new ArgumentNullException(nameof(admDivQuery));
        }

        /// <summary>
        /// 获取行政区划列表
        /// </summary>
        /// <param name="admDivKey">行政区划关键字（可选）</param>
        /// <param name="includeSubNodes">是否包含子孙节点（可选）</param>
        /// <param name="cancellationToken">取消操作的标记</param>
        /// <returns>行政区划集合</returns>
        [CheckPermission]
        [HttpGet("list")]
        [ProducesResponseType(typeof(IEnumerable<AdmDiv>), StatusCodes.Status200OK)]
        [AuditLogActionFilter("获取行政区划列表")]
        public async Task<ActionResult<IEnumerable<AdmDiv>>> GetListAsync(
            [FromQuery] string admDivKey = "",
            [FromQuery] bool includeSubNodes = false,
            CancellationToken cancellationToken = default)
        {
            var result = await _admDivQuery.GetListAsync(admDivKey, includeSubNodes, cancellationToken);
            return Ok(result);
        }

        /// <summary>
        /// 刷新行政区划缓存
        /// </summary>
        /// <param name="cancellationToken">取消操作的标记</param>
        /// <returns>操作结果</returns>
        [CheckPermission(Permissions.CreateAndUpdateAdministrativeDivision)]
        [HttpPost("refresh")]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        [AuditLogActionFilter("刷新行政区划缓存")]
        public async Task<IActionResult> RefreshAsync(CancellationToken cancellationToken = default)
        {
            await _admDivQuery.RefreshAsync(cancellationToken);
            return NoContent();
        }
    }
}